﻿var GoogleAnalytics = (function(config) {
	var _initialized = false;

	// return a unique name for the tracker
	// there should never be more than two or three (but most likely only one) of these
	// registered, so we don't exactly have to go through any heroic efforts here
	// iterating through lower-case letters should suffice.
	var _lastPrefix = 96;
	var _generatePrefix = function() {
		_lastPrefix++;
		return String.fromCharCode(_lastPrefix);
	};

	var _init = function() {
		if (!_initialized) {
			window._gaq = window._gaq || [];
			(function() {
				var ga = document.createElement('script');
				ga.type = 'text/javascript';
				ga.async = true;
				ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var s = document.getElementsByTagName('script')[0];
				s.parentNode.insertBefore(ga, s);
			})();
			_initialized = true;
		}
	};

	return {
		create: function(config) {
			var prefix = _generatePrefix();
			_init(); // call to the outer closure init method since we only want to load script once

			// now do instance-specific config
			_gaq.push([prefix + '._setAccount', config.accountID]);
			return {
				track: function(url) {
					_gaq.push([prefix + '._trackPageview', url]);
				}
			};
		}
	};
})();